// pages/play/play.js
var api = require('../../utils/api.js');
var app = getApp();
const bgam = wx.getBackgroundAudioManager();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      currentzj:{},
      audioList:[],
      audio_index:0,
      currentIntervalId:0,
      errCode:0,
      currentAudio:{
        index:0,
        zjTitle:"",
        zjDetail:"",
        zjImageUrl:"",
        name:"",
        audioSrc:"",
        sumTime:"",
        sumTime_sec:0,
        nowTime:"",
        nowTime_sec:0,
      },
      hidden_flag:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var _album = wx.getStorageSync("album");
      wx.removeStorageSync("album");
      if(bgam.src != '' && _album != ''){
          //为page变量赋值
          this.audioList = _album.audiolist;
          this.audio_index = _album.curAudioIndex;
          this.currentzj =_album.albumInfo;
          this.setData({
              'currentAudio.name' : this.audioList[this.audio_index].name,
              'currentAudio.audioSrc' : this.audioList[this.audio_index].url,
              'currentAudio.index' : this.audio_index,
              'currentAudio.zjTitle' : this.currentzj.name,
              'currentAudio.zjImageUrl' : this.currentzj.thumbnail,
              'currentAudio.zjDetail' : this.currentzj.description,
              'audioList':_album.audiolist
          });
          var currentAudio = this.data.currentAudio;
          bgam.src = currentAudio.audioSrc;
          bgam.title = currentAudio.name;
          bgam.epname = currentAudio.zjTitle;
          bgam.coverImgUrl = currentAudio.zjImageUrl;
          bgam.onEnded(()=>{
              this.nextAudio();
          })
          bgam.onPlay(()=>{
              //更新收听次数
              api.get({
                  url:'portal/album/count',
                  data:{'album_id':this.currentzj.id},
                  success:(data)=>{
                      console.log(data);
                  }
              });
              wx.setStorage({
                  key:"currentAudio",
                  data:this.data.currentAudio
              });
          })
          bgam.play();

          //异步获取播放时间
          this.currentIntervalId = setInterval(()=>{
              var _sumtime = Math.ceil(bgam.duration);
              var _nowtime = Math.ceil(bgam.currentTime);
              currentAudio.sumTime_sec = bgam.duration;
              currentAudio.nowTime_sec = bgam.currentTime;
              currentAudio.sumTime = Math.floor(_sumtime/60)+":"+(_sumtime%60/100).toFixed(2).slice(-2);
              currentAudio.nowTime = Math.floor(_nowtime/60)+":"+(_nowtime%60/100).toFixed(2).slice(-2);
              this.setData({
                  'currentAudio.sumTime' : currentAudio.sumTime,
                  'currentAudio.nowTime' : currentAudio.nowTime
              });
          },1000);
      }

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
    /**
     * 点击播放
     * @param e
     */
    play:function(e){
        //play按钮隐藏

        //异步获取播放时间
        if(this.currentIntervalId != 0) {
            clearInterval(this.currentIntervalId);
        }
        this.setData({hidden_flag:0});
            var currentAudio = this.data.currentAudio;
            this.currentIntervalId = setInterval(()=>{
                var _sumtime = Math.ceil(bgam.duration);
                var _nowtime = Math.ceil(bgam.currentTime);
                currentAudio.sumTime_sec = bgam.duration;
                currentAudio.nowTime_sec = bgam.currentTime;
                currentAudio.sumTime = Math.floor(_sumtime/60)+":"+(_sumtime%60/100).toFixed(2).slice(-2);
                currentAudio.nowTime = Math.floor(_nowtime/60)+":"+(_nowtime%60/100).toFixed(2).slice(-2);
                this.setData({
                    'currentAudio.sumTime' : currentAudio.sumTime,
                    'currentAudio.nowTime' : currentAudio.nowTime
                });
            },1000);
        var currentAudio = this.data.currentAudio;
        bgam.src = currentAudio.audioSrc;
        bgam.title = currentAudio.name;
        bgam.epname = currentAudio.zjTitle;
        bgam.startTime = currentAudio.nowTime_sec;
        bgam.play();
    },
    /**
     * 用户点击播放清单，播放由首页进来的id序号开始播放
     * @param e
     */
    playListTap:function(e){
        var _index = e.currentTarget.dataset.id;
        if(this.currentIntervalId != 0) {
            clearInterval(this.currentIntervalId);
        }
        bgam.stop();
        var _currentAudio = this.audioList[_index];
        //重新赋值当前音乐
        this.setData({
            'currentAudio.name' : _currentAudio.name,
            'currentAudio.audioSrc' : _currentAudio.url,
            'currentAudio.index' : _index
        });
        var currentAudio = this.data.currentAudio;
        bgam.src = currentAudio.audioSrc;
        bgam.title = currentAudio.name;
        bgam.startTime = 0;
        bgam.play();
        //启动更新定时
        this.currentIntervalId = setInterval(()=>{
            var _sumtime = Math.ceil(bgam.duration);
            var _nowtime = Math.ceil(bgam.currentTime);
            currentAudio.sumTime_sec = bgam.duration;
            currentAudio.nowTime_sec = bgam.currentTime;
            currentAudio.sumTime = Math.floor(_sumtime/60)+":"+(_sumtime%60/100).toFixed(2).slice(-2);
            currentAudio.nowTime = Math.floor(_nowtime/60)+":"+(_nowtime%60/100).toFixed(2).slice(-2);
            this.setData({
                'currentAudio.sumTime' : currentAudio.sumTime,
                'currentAudio.nowTime' : currentAudio.nowTime
            });
        },1000);
    },
    /**
     * 下一曲
     * @param e
     */
    nextAudio:function(e){
        var _end = this.audioList.length;
        if(this.audio_index < _end){
            this.audio_index += 1;
            if(this.currentIntervalId != 0) {
                clearInterval(this.currentIntervalId);
            }

            bgam.stop();
            var _currentAudio = this.audioList[this.audio_index];
            //重新赋值当前音乐
            this.setData({
                'currentAudio.name' : _currentAudio.name,
                'currentAudio.audioSrc' : _currentAudio.url,
                'currentAudio.index' : this.audio_index
            });
            //更新本地数据


            var currentAudio = this.data.currentAudio;
            bgam.src = currentAudio.audioSrc;
            bgam.title = currentAudio.name;
            bgam.startTime = 0;
            bgam.play();
            //启动更新定时
            this.currentIntervalId = setInterval(()=>{
                var _sumtime = Math.ceil(bgam.duration);
                var _nowtime = Math.ceil(bgam.currentTime);
                currentAudio.sumTime_sec = bgam.duration;
                currentAudio.nowTime_sec = bgam.currentTime;
                currentAudio.sumTime = Math.floor(_sumtime/60)+":"+(_sumtime%60/100).toFixed(2).slice(-2);
                currentAudio.nowTime = Math.floor(_nowtime/60)+":"+(_nowtime%60/100).toFixed(2).slice(-2);
                this.setData({
                    'currentAudio.sumTime' : currentAudio.sumTime,
                    'currentAudio.nowTime' : currentAudio.nowTime
                });
            },1000);
        }
    },
    /**
     * 上一曲
     * @param e
     */
    preAudio:function(e){
        var _end = this.audioList.length;
        if(this.audio_index > 0){
            this.audio_index -= 1;
            if(this.currentIntervalId != 0) {
                clearInterval(this.currentIntervalId);
            }
            bgam.stop();
            var _currentAudio = this.audioList[this.audio_index];
            //重新赋值当前音乐
            this.setData({
                'currentAudio.name' : _currentAudio.name,
                'currentAudio.audioSrc' : _currentAudio.url,
                'currentAudio.index' : this.audio_index
            });
            var currentAudio = this.data.currentAudio;
            bgam.src = currentAudio.audioSrc;
            bgam.title = currentAudio.name;
            bgam.startTime = 0;
            bgam.play();
            //启动更新定时
            this.currentIntervalId = setInterval(()=>{
                var _sumtime = Math.ceil(bgam.duration);
                var _nowtime = Math.ceil(bgam.currentTime);
                currentAudio.sumTime_sec = bgam.duration;
                currentAudio.nowTime_sec = bgam.currentTime;
                currentAudio.sumTime = Math.floor(_sumtime/60)+":"+(_sumtime%60/100).toFixed(2).slice(-2);
                currentAudio.nowTime = Math.floor(_nowtime/60)+":"+(_nowtime%60/100).toFixed(2).slice(-2);
                this.setData({
                    'currentAudio.sumTime' : currentAudio.sumTime,
                    'currentAudio.nowTime' : currentAudio.nowTime
                });
            },1000);
        }
    },
    stopAudio:function(e){
      this.setData({hidden_flag:1});
        if(this.currentIntervalId != 0) {
            clearInterval(this.currentIntervalId);
        }
      this.errCode = bgam.stop();
    },
    showModal: function (e) {
        var zjid = e.currentTarget.dataset.zjid;
        var file = this.audioList[zjid].files;
        bgam.src = file.url;
        bgam.play();
    },
    closeModal: function (e) {
        this.setData({
            modalName: null
        })
    }

})